﻿// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file, You can obtain one at https://opensource.org/licenses/MIT.
// Copyright (C) Leszek Pomianowski and WPF UI Contributors.
// All Rights Reserved.

using Se.CommunityToolkit.Module;

namespace Wpf.Ui.Gallery.Services;

public class ModuleService
{
    private readonly IServiceCollection _services;
    private readonly ObservableCollection<object> _menuItems;
    public ModuleService(IServiceCollection services)
    {
        _services = services;
    }
    
   
    public ModuleService( ObservableCollection<object> menuItems)
    {
        _menuItems = menuItems;
    }

    public void AddModules()
    {
        var modules = GetModulesDirs();
        foreach (var module in modules)
        {
            var di = new DirectoryInfo(module);
            FileInfo? moduleDll = di.GetFiles("*.Module.dll").FirstOrDefault();
            if (moduleDll != null)
            {
                AddModule(moduleDll.FullName);
            }
        }
    }

    private void AddModule(string modulePath)
    {
        var ass = Assembly.LoadFile(modulePath); //要绝对路径

        Type[] types = ass.GetTypes();
        foreach (Type type in types)
        {
            if (!type.IsClass)
            {
                continue;
            }

            var t = Attribute.GetCustomAttribute(type, typeof(SeCommunityToolkitModuleAttribute));
            if (t is SeCommunityToolkitModuleAttribute && type.FullName != null)
            {
                var obj = ass.CreateInstance(type.FullName);
                MethodInfo? meth = type.GetMethod("AddModule");
                if (meth != null)
                {
                    meth.Invoke(obj, new object[] {_menuItems});
                }
            }
        }
    }
    
    public void AddService()
    {
        var modules = GetModulesDirs();
        foreach (var module in modules)
        {
            var di = new DirectoryInfo(module);
            FileInfo? moduleDll = di.GetFiles("*.Module.dll").FirstOrDefault();
            if (moduleDll != null)
            {
                AddService(moduleDll.FullName);
            }
        }
    }
    
    private void AddService(string modulePath)
    {
        var ass = Assembly.LoadFile(modulePath); //要绝对路径

        Type[] types = ass.GetTypes();
        foreach (Type type in types)
        {
            if (!type.IsClass)
            {
                continue;
            }

            var t = Attribute.GetCustomAttribute(type, typeof(SeCommunityToolkitModuleAttribute));
            if (t is SeCommunityToolkitModuleAttribute && type.FullName != null)
            {
                var obj = ass.CreateInstance(type.FullName);
                MethodInfo? meth2 = type.GetMethod("AddService");
                if (meth2 != null)
                {
                    meth2.Invoke(obj, new object[] {_services});
                }
            }
        }
    }

    private string[] GetModulesDirs()
    {
        var modules = Directory.GetDirectories($@"{AppDomain.CurrentDomain.BaseDirectory}Modules\");
        return modules;
    }
}